package com.acmen.easyapi.parse;

import com.acmen.easyapi.model.Cell;
import com.acmen.easyapi.model.ResParamNode;
import com.acmen.easyapi.model.Table;
import com.acmen.easyapi.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author acmen
 * 详细表格输出执行器
 */
@Component
public class ResponseTableParser {
    public List<Table> parseToTables(ResParamNode resParamNode){
        List<ResParamNode> subNodes = resParamNode.getSubNodes();
        List<Table> tables = new ArrayList<>();
        if(subNodes==null||subNodes.isEmpty()){
            return tables;
        }
        String title = resParamNode.getLabel();
        if(resParamNode.isArray()){
            title += "("+resParamNode.getTypeAlias()+")";
        }
        Table table = new Table(UUID.randomUUID().toString(),title);
        tables.add(table);
        for(ResParamNode node : subNodes){
            List<Cell> cells = new ArrayList<Cell>();
            List<Table> subTables = parseToTables(node);
            Cell cell = new Cell(node.getName());
            if (!subTables.isEmpty()) {
                tables.addAll(subTables);
                cell.setHref("#" + subTables.get(0).getId());
            }
            if(node.isDeprecated()){
                cell.setStyle("text-decoration:line-through;color:red;");
            }
            cells.add(cell);
            cells.add(new Cell(node.getLabel()));
            cells.add(new Cell(node.getTypeAlias()));
            cells.add(new Cell(node.getClient()));
            String example = "";
            if(StringUtil.isNotEmpty(node.getExample())){
                example = node.getExample().toString();
            }else if(StringUtil.isNotEmpty(node.getExample2())){
                example = node.getExample2().toString();
            }
            cell = new Cell(example);
            cell.setStyle("text-align:left");
            cells.add(cell);
            cell = new Cell(NoteBuilder.build(node.getNotes(),node.getDictionaries()));
            cell.setStyle("text-align:left");
            cells.add(cell);
            table.add(cells);
        }
        return tables;
    }
}
